<?xml version="1.0" encoding="ascii"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
          "DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <title>genshi.template.eval.Expression</title>
  <link rel="stylesheet" href="epydoc.css" type="text/css" />
  <script type="text/javascript" src="epydoc.js"></script>
</head>

<body bgcolor="white" text="black" link="blue" vlink="#204080"
      alink="#204080">
<!-- ==================== NAVIGATION BAR ==================== -->
<table class="navbar" border="0" width="100%" cellpadding="0"
       bgcolor="#a0c0ff" cellspacing="0">
  <tr valign="middle">
  <!-- Home link -->
      <th>&nbsp;&nbsp;&nbsp;<a
        href="genshi-module.html">Home</a>&nbsp;&nbsp;&nbsp;</th>

  <!-- Tree link -->
      <th>&nbsp;&nbsp;&nbsp;<a
        href="module-tree.html">Trees</a>&nbsp;&nbsp;&nbsp;</th>

  <!-- Index link -->
      <th>&nbsp;&nbsp;&nbsp;<a
        href="identifier-index.html">Indices</a>&nbsp;&nbsp;&nbsp;</th>

  <!-- Help link -->
      <th>&nbsp;&nbsp;&nbsp;<a
        href="help.html">Help</a>&nbsp;&nbsp;&nbsp;</th>

  <!-- Project homepage -->
      <th class="navbar" align="right" width="100%">
        <table border="0" cellpadding="0" cellspacing="0">
          <tr><th class="navbar" align="center"
            ><a class="navbar" target="_top" href="../index.html">Documentation Index</a></th>
          </tr></table></th>
  </tr>
</table>
<table width="100%" cellpadding="0" cellspacing="0">
  <tr valign="top">
    <td width="100%">
      <span class="breadcrumbs">
        <a href="genshi-module.html">Package&nbsp;genshi</a> ::
        <a href="genshi.template-module.html">Package&nbsp;template</a> ::
        <a href="genshi.template.eval-module.html">Module&nbsp;eval</a> ::
        Class&nbsp;Expression
      </span>
    </td>
    <td>
      <table cellpadding="0" cellspacing="0">
        <!-- hide/show private -->
      </table>
    </td>
  </tr>
</table>
<!-- ==================== CLASS DESCRIPTION ==================== -->
<h1 class="epydoc">Class Expression</h1><p class="nomargin-top"></p>
<pre class="base-tree">
object --+    
         |    
      <a href="genshi.template.eval.Code-class.html">Code</a> --+
             |
            <strong class="uidshort">Expression</strong>
</pre>

<hr />
<p>Evaluates Python expressions used in templates.</p>
<pre class="py-doctest">
<span class="py-prompt">&gt;&gt;&gt; </span>data = dict(test=<span class="py-string">'Foo'</span>, <span class="py-builtin">items</span>=[1, 2, 3], dict={<span class="py-string">'some'</span>: <span class="py-string">'thing'</span>})
<span class="py-prompt">&gt;&gt;&gt; </span>Expression(<span class="py-string">'test'</span>).evaluate(data)
<span class="py-output">'Foo'</span></pre>
<pre class="py-doctest">
<span class="py-prompt">&gt;&gt;&gt; </span>Expression(<span class="py-string">'items[0]'</span>).evaluate(data)
<span class="py-output">1</span>
<span class="py-output"></span><span class="py-prompt">&gt;&gt;&gt; </span>Expression(<span class="py-string">'items[-1]'</span>).evaluate(data)
<span class="py-output">3</span>
<span class="py-output"></span><span class="py-prompt">&gt;&gt;&gt; </span>Expression(<span class="py-string">'dict[&quot;some&quot;]'</span>).evaluate(data)
<span class="py-output">'thing'</span></pre>
<p>Similar to e.g. Javascript, expressions in templates can use the dot
notation for attribute access to access items in mappings:</p>
<pre class="py-doctest">
<span class="py-prompt">&gt;&gt;&gt; </span>Expression(<span class="py-string">'dict.some'</span>).evaluate(data)
<span class="py-output">'thing'</span></pre>
<p>This also works the other way around: item access can be used to access
any object attribute:</p>
<pre class="py-doctest">
<span class="py-prompt">&gt;&gt;&gt; </span><span class="py-keyword">class</span> <span class="py-defname">MyClass</span>(object):
<span class="py-more">... </span>    myattr = <span class="py-string">'Bar'</span>
<span class="py-prompt">&gt;&gt;&gt; </span>data = dict(mine=MyClass(), key=<span class="py-string">'myattr'</span>)
<span class="py-prompt">&gt;&gt;&gt; </span>Expression(<span class="py-string">'mine.myattr'</span>).evaluate(data)
<span class="py-output">'Bar'</span>
<span class="py-output"></span><span class="py-prompt">&gt;&gt;&gt; </span>Expression(<span class="py-string">'mine[&quot;myattr&quot;]'</span>).evaluate(data)
<span class="py-output">'Bar'</span>
<span class="py-output"></span><span class="py-prompt">&gt;&gt;&gt; </span>Expression(<span class="py-string">'mine[key]'</span>).evaluate(data)
<span class="py-output">'Bar'</span></pre>
<p>All of the standard Python operators are available to template expressions.
Built-in functions such as <tt class="rst-docutils literal"><span class="pre">len()</span></tt> are also available in template
expressions:</p>
<pre class="py-doctest">
<span class="py-prompt">&gt;&gt;&gt; </span>data = dict(<span class="py-builtin">items</span>=[1, 2, 3])
<span class="py-prompt">&gt;&gt;&gt; </span>Expression(<span class="py-string">'len(items)'</span>).evaluate(data)
<span class="py-output">3</span></pre>

<!-- ==================== INSTANCE METHODS ==================== -->
<a name="section-InstanceMethods"></a>
<table class="summary" border="1" cellpadding="3"
       cellspacing="0" width="100%" bgcolor="white">
<tr bgcolor="#70b0f0" class="table-header">
  <td align="left" colspan="2" class="table-header">
    <span class="table-header">Instance Methods</span></td>
</tr>
<tr>
    <td width="15%" align="right" valign="top" class="summary">
      <span class="summary-type">&nbsp;</span>
    </td><td class="summary">
      <table width="100%" cellpadding="0" cellspacing="0" border="0">
        <tr>
          <td><span class="summary-sig"><a href="genshi.template.eval.Expression-class.html#evaluate" class="summary-sig-name">evaluate</a>(<span class="summary-sig-arg">self</span>,
        <span class="summary-sig-arg">data</span>)</span><br />
      Evaluate the expression against the given data dictionary.</td>
          <td align="right" valign="top">
            
            
          </td>
        </tr>
      </table>
      
    </td>
  </tr>
  <tr>
    <td colspan="2" class="summary">
    <p class="indent-wrapped-lines"><b>Inherited from <code><a href="genshi.template.eval.Code-class.html">Code</a></code></b>:
      <code><a href="genshi.template.eval.Code-class.html#__eq__">__eq__</a></code>,
      <code><a href="genshi.template.eval.Code-class.html#__getstate__">__getstate__</a></code>,
      <code><a href="genshi.template.eval.Code-class.html#__hash__">__hash__</a></code>,
      <code><a href="genshi.template.eval.Code-class.html#__init__">__init__</a></code>,
      <code><a href="genshi.template.eval.Code-class.html#__ne__">__ne__</a></code>,
      <code><a href="genshi.template.eval.Code-class.html#__repr__">__repr__</a></code>,
      <code><a href="genshi.template.eval.Code-class.html#__setstate__">__setstate__</a></code>
      </p>
    <p class="indent-wrapped-lines"><b>Inherited from <code>object</code></b>:
      <code>__delattr__</code>,
      <code>__getattribute__</code>,
      <code>__new__</code>,
      <code>__reduce__</code>,
      <code>__reduce_ex__</code>,
      <code>__setattr__</code>,
      <code>__str__</code>
      </p>
    </td>
  </tr>
</table>
<!-- ==================== CLASS VARIABLES ==================== -->
<a name="section-ClassVariables"></a>
<table class="summary" border="1" cellpadding="3"
       cellspacing="0" width="100%" bgcolor="white">
<tr bgcolor="#70b0f0" class="table-header">
  <td align="left" colspan="2" class="table-header">
    <span class="table-header">Class Variables</span></td>
</tr>
<tr>
    <td width="15%" align="right" valign="top" class="summary">
      <span class="summary-type">&nbsp;</span>
    </td><td class="summary">
        <a name="mode"></a><span class="summary-name">mode</span> = <code title="'eval'"><code class="variable-quote">'</code><code class="variable-string">eval</code><code class="variable-quote">'</code></code>
    </td>
  </tr>
</table>
<!-- ==================== PROPERTIES ==================== -->
<a name="section-Properties"></a>
<table class="summary" border="1" cellpadding="3"
       cellspacing="0" width="100%" bgcolor="white">
<tr bgcolor="#70b0f0" class="table-header">
  <td align="left" colspan="2" class="table-header">
    <span class="table-header">Properties</span></td>
</tr>
  <tr>
    <td colspan="2" class="summary">
    <p class="indent-wrapped-lines"><b>Inherited from <code><a href="genshi.template.eval.Code-class.html">Code</a></code></b>:
      <code><a href="genshi.template.eval.Code-class.html#ast">ast</a></code>,
      <code><a href="genshi.template.eval.Code-class.html#code">code</a></code>,
      <code><a href="genshi.template.eval.Code-class.html#source">source</a></code>
      </p>
    <p class="indent-wrapped-lines"><b>Inherited from <code>object</code></b>:
      <code>__class__</code>
      </p>
    </td>
  </tr>
</table>
<!-- ==================== METHOD DETAILS ==================== -->
<a name="section-MethodDetails"></a>
<table class="details" border="1" cellpadding="3"
       cellspacing="0" width="100%" bgcolor="white">
<tr bgcolor="#70b0f0" class="table-header">
  <td align="left" colspan="2" class="table-header">
    <span class="table-header">Method Details</span></td>
</tr>
</table>
<a name="evaluate"></a>
<div>
<table class="details" border="1" cellpadding="3"
       cellspacing="0" width="100%" bgcolor="white">
<tr><td>
  <table width="100%" cellpadding="0" cellspacing="0" border="0">
  <tr valign="top"><td>
  <h3 class="epydoc"><span class="sig"><span class="sig-name">evaluate</span>(<span class="sig-arg">self</span>,
        <span class="sig-arg">data</span>)</span>
  </h3>
  </td><td align="right" valign="top"
    >&nbsp;
    </td>
  </tr></table>
  
  Evaluate the expression against the given data dictionary.
  <dl class="fields">
    <dt>Parameters:</dt>
    <dd><ul class="nomargin-top">
        <li><strong class="pname"><code>data</code></strong> - a mapping containing the data to evaluate against</li>
    </ul></dd>
    <dt>Returns:</dt>
        <dd>the result of the evaluation</dd>
  </dl>
</td></tr></table>
</div>
<br />
<!-- ==================== NAVIGATION BAR ==================== -->
<table class="navbar" border="0" width="100%" cellpadding="0"
       bgcolor="#a0c0ff" cellspacing="0">
  <tr valign="middle">
  <!-- Home link -->
      <th>&nbsp;&nbsp;&nbsp;<a
        href="genshi-module.html">Home</a>&nbsp;&nbsp;&nbsp;</th>

  <!-- Tree link -->
      <th>&nbsp;&nbsp;&nbsp;<a
        href="module-tree.html">Trees</a>&nbsp;&nbsp;&nbsp;</th>

  <!-- Index link -->
      <th>&nbsp;&nbsp;&nbsp;<a
        href="identifier-index.html">Indices</a>&nbsp;&nbsp;&nbsp;</th>

  <!-- Help link -->
      <th>&nbsp;&nbsp;&nbsp;<a
        href="help.html">Help</a>&nbsp;&nbsp;&nbsp;</th>

  <!-- Project homepage -->
      <th class="navbar" align="right" width="100%">
        <table border="0" cellpadding="0" cellspacing="0">
          <tr><th class="navbar" align="center"
            ><a class="navbar" target="_top" href="../index.html">Documentation Index</a></th>
          </tr></table></th>
  </tr>
</table>
<table border="0" cellpadding="0" cellspacing="0" width="100%%">
  <tr>
    <td align="left" class="footer">
    Generated by Epydoc 3.0.1 on Wed Jul  9 18:16:21 2008
    </td>
    <td align="right" class="footer">
      <a target="mainFrame" href="http://epydoc.sourceforge.net"
        >http://epydoc.sourceforge.net</a>
    </td>
  </tr>
</table>

<script type="text/javascript">
  <!--
  // Private objects are initially displayed (because if
  // javascript is turned off then we want them to be
  // visible); but by default, we want to hide them.  So hide
  // them unless we have a cookie that says to show them.
  checkCookie();
  // -->
</script>
</body>
</html>
